package com.zheng.oa.admin.controller.hr;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zheng.common.base.BaseController;
import com.zheng.common.util.CamelUnderlineConverter;
import com.zheng.hr.dao.model.HrFestival;
import com.zheng.hr.rpc.api.HrFestivalService;
import com.zheng.oa.common.constant.OaResult;
import com.zheng.oa.common.constant.OaResultConstant;
import com.zheng.oa.common.exception.OaException;
import com.zheng.upms.rpc.api.UpmsApiService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: xinqi
 * Date: 2018/6/2
 * Time: 9:21
 */
@Controller
@RequestMapping("/oa/hr/festival")
public class HrFestivalController extends BaseController {
    @Autowired
    private HrFestivalService festivalService;
    @Autowired
    private UpmsApiService apiService;

    @RequiresPermissions("oa:hr:festival:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        return "/manage/hr/festival/index.jsp";
    }

    @RequiresPermissions("oa:hr:festival:read")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Object list(@RequestParam(required = false, defaultValue = "1", value = "pageNumber") int pageNumber,
                       @RequestParam(required = false, defaultValue = "10", value = "pageSize") int pageSize,
                       @RequestParam(required = false, value = "sortName") String sortName,
                       @RequestParam(required = false, value = "sortOrder") String sortOrder,
                       HrFestival festival) {
        Wrapper<HrFestival> wrapper = new EntityWrapper<>();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            if (sortOrder.equalsIgnoreCase("asc")) {
                wrapper = wrapper.orderBy(CamelUnderlineConverter.camel2underline(sortName), true);
            } else {
                wrapper = wrapper.orderBy(CamelUnderlineConverter.camel2underline(sortName), false);
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String year = sdf.format(festival.getFestivalDate());
        if (festival.getFestivalDate() != null) {
            wrapper.eq("DATE_FORMAT(festival_date,'%Y')", year);
        }
        wrapper = apiService.dataScopeFilter(wrapper, getUsername());
        Page<HrFestival> page = festivalService.selectPage(new Page<>(pageNumber, pageSize), wrapper);
        int total = festivalService.selectCount(wrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", page.getRecords());
        result.put("total", total);
        return result;
    }

    @RequiresPermissions("oa:hr:festival:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "/manage/hr/festival/create.jsp";
    }

    @RequiresPermissions("oa:hr:festival:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(@RequestBody HrFestival festival) {
        try {
            festivalService.insert(festival);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }

    @RequiresPermissions("oa:hr:festival:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Integer id, Model model) {
        HrFestival festival = festivalService.selectById(id);
        model.addAttribute("festival", festival);
        return "/manage/hr/festival/update.jsp";
    }

    @RequiresPermissions("oa:hr:festival:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(@RequestBody HrFestival festival) {
        try {
            festivalService.updateById(festival);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }

    @RequiresPermissions("oa:hr:festival:delete")
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        try {
            festivalService.deleteBatchIds(ids);
        } catch (Exception e) {
            return new OaResult(OaResultConstant.FAILED, e.getMessage());
        }
        return new OaResult(OaResultConstant.SUCCESS, null);
    }
}
